﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;
using System.Text;
using System;
using com.guojin.mj.net.message.game;
public class LiuYanPanel : EventManager {
    public Button Ret,but1,but2,OK;
    public Button picture1;
    public RawImage Photo;
    public int RetType=1;
    public string picture="a";
    public InputField acc, said;
    public GameObject bg;
    public override void InformationSetting()
    {
    }
    void Start () {
        Ret.onClick.AddListener(delegate {
            Destroy(this.gameObject);
        });
        AddListenerToButton(but1,ClickBut1);
        AddListenerToButton(but2, ClickBut2);
        AddListenerToButton(picture1, OnPicture11);
        AddListenerToButton(OK, TiJiao);
    }
    void ClickBut1() {
        but2.GetComponent<Image>().color = new Color(1,0,142/255f,1);
        but1.GetComponent<Image>().color = new Color(1, 1, 1 , 1);
        RetType = 1;
    }
    void ClickBut2()
    {
        but2.GetComponent<Image>().color = new Color(1, 1, 1 , 1);
        but1.GetComponent<Image>().color = new Color(1, 0, 142 / 255f, 1);
        RetType = 2;
    }
    void OnPicture11() {
        LoginAndShare.Controller.OpenPhoto(0);
    }
    void TiJiao()
    {
        if (acc.text == null || acc.text == "" || said.text == "" || said.text == null)
        {
            TooL.WenXinTiSi("账号和问题不能为空");
            return;
        }
        if (picture != null && picture != "a"&& picture !="")
        {
            string param = "{ \"type\":\"img\",\"data\":\"data:image/png;base64," + picture + "\"}";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("data", param);
            string result = HttpPostData(ServerAdress, dic);
            //服务器返回语音地址
            Debug.Log("服务器返回地址:" + result);
            bg.SetActive(true);
            MessageManager.GetInstance().Send7155(RetType, said.text, result, acc.text);
        }
        else
        {
            TooL.WenXinTiSi("请选择图片");
        }
    }
    private string ServerAdress = "http://121.40.90.67:8092/up";
    //这个是收到1.25   4类型代表语音
    public int msgType = 4;

    //发送录音文件字节流，将返回字节流广播出去
    public void Send(byte[] data)
    {
        string voiceStr = Convert.ToBase64String(data);
        string param = "{ \"type\":\"img\",\"data\":\"data:image/png;base64," + voiceStr + "\"}";

        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("data", param);
        string result = HttpPostData(ServerAdress, dic);

        //服务器返回语音地址
        Debug.Log("服务器返回地址:" + result);

        MessageManager.GetInstance().SendMsg1224(result, msgType);
    }
    //发送录音字节流 收到服务器返回网址
    public string HttpPostData(string url, Dictionary<string, string> dic)
    {
        var result = string.Empty;

        //#region 添加Post 参数  
        StringBuilder builder = new StringBuilder();
        int i = 0;
        foreach (var item in dic)
        {
            if (i > 0)
                builder.Append("&");
            builder.AppendFormat("{0}={1}", item.Key, item.Value);
            i++;
        }
        byte[] postData = Encoding.UTF8.GetBytes(builder.ToString());
        // 设置提交的相关参数 

        System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
        Encoding myEncoding = Encoding.UTF8;
        request.Method = "POST";
        request.KeepAlive = false;
        request.AllowAutoRedirect = true;
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
        request.ContentLength = postData.Length;
        request.Timeout = 3000;

        // 提交请求数据 
        System.IO.Stream outputStream = request.GetRequestStream();
        outputStream.Write(postData, 0, postData.Length);
        outputStream.Close();

        System.Net.HttpWebResponse response;
        Stream responseStream;
        StreamReader reader;
        string srcString;
        response = request.GetResponse() as System.Net.HttpWebResponse;
        responseStream = response.GetResponseStream();
        reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
        srcString = reader.ReadToEnd();
        result = srcString;   //返回值赋值
        reader.Close();
        Debug.Log("获取HttpPostData服务器返回Version" + result);
        return result;
    }
}
